package com.qiwx.pigeon.activity.home;

import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.Spinner;
import android.widget.SpinnerAdapter;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.andview.refreshview.XRefreshView;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemLongClickListener;
import com.qiwx.pigeon.R;
import com.qiwx.pigeon.Utils.DBSqlUtil;
import com.qiwx.pigeon.Utils.MyToast;
import com.qiwx.pigeon.Utils.NumberUtil;
import com.qiwx.pigeon.activity.MainActivity;
import com.qiwx.pigeon.adapter.home.EliminateAdapter;
import com.qiwx.pigeon.adapter.home.PdStatusAdapter;
import com.qiwx.pigeon.adapter.home.PigeonPairAdapter;
import com.qiwx.pigeon.base.BaseActivity;
import com.qiwx.pigeon.data.CageStatusConfig;
import com.qiwx.pigeon.data.db.PigeonPairModel;
import com.qiwx.pigeon.data.params.CLZTParam;
import com.qiwx.pigeon.data.params.CageParam;
import com.qiwx.pigeon.data.vo.CageBeanVo;
import com.qiwx.pigeon.data.vo.CageStatusRecordBean;
import com.qiwx.pigeon.data.vo.FertilizeBean;
import com.qiwx.pigeon.data.vo.PairBean;
import com.qiwx.pigeon.data.vo.PigeonPiarBean;
import com.qiwx.pigeon.data.vo.WareHouseBean;
import com.qiwx.pigeon.data.vo.pageinfo.PageInfo;
import com.qiwx.pigeon.helper.DialogHelper;
import com.qiwx.pigeon.presenter.OperatePresenter;
import com.qiwx.pigeon.viewinterface.IOperateView;
import com.qiwx.pigeon.widget.ArrowView;
import com.qiwx.pigeon.widget.itemderection.LineItemDecoration;

import org.angmarch.views.NiceSpinner;
import org.litepal.LitePal;

import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @description
 * @Author qiwx
 * @Date 2020-04-05 16:55
 * 鸽笼配对
 **/
public class PigeonPairActivity extends BaseRemindActivity implements IOperateView {

    List<PairBean> data = new ArrayList<>();
    CageParam cageParam = new CageParam();
    int current_pos = -1;
    Spinner spinnerStatus;
    List<String> statusItems;
    Map<String, Integer> maps = new LinkedHashMap<String, Integer>();
    int status = -1;
    OperatePresenter operatePresenter;


    @Override
    public void initAfterOnCreate() {
        cageParam.setPageSize(pageSize);
        initMap();
        Set<String> sets = maps.keySet();
        statusItems = new ArrayList<>(sets);
        initView();
        operatePresenter = new OperatePresenter(new WeakReference(this));
        // xRefreshView.startRefresh();
    }

    private void initMap() {
        maps.put("不限", -1);
        maps.put("空笼", CageStatusConfig.STATUS_KL);
        maps.put("配对中", CageStatusConfig.STATUS_PD);
        maps.put("单只", CageStatusConfig.STATUS_DZ);
        maps.put("单公", CageStatusConfig.STATUS_DG);
        maps.put("单母", CageStatusConfig.STATUS_DM);
        maps.put("双公", CageStatusConfig.STATUS_SG);
        maps.put("双母", CageStatusConfig.STATUS_SM);

    }

    @Override
    public void loadData(boolean isRefresh) {
        if (isRefresh) {
            pageNo = 1;
            data.clear();
        } else {
            pageNo++;
        }
        cageParam.setWh_id(wh_id);
        cageParam.setStatus(status);
        mPresenter.queryCagePairByPage(cageParam);
    }

    @Override
    public BaseQuickAdapter getAdapter() {
        adapter = new PigeonPairAdapter(R.layout.adapter_remind_pair_list_item, data);
        return adapter;
    }

    @Override
    public int getSize() {
        return data.size();
    }

    @Override
    public int addListViewHead() {
        return R.layout.cage_remind_pair_headview;
    }

    @Override
    public void initView() {
        super.initView();
        titleReturn.changeArrowLeft();
        titleContent.setText("鸽笼配对详情");

        if (adapter != null && adapter instanceof PigeonPairAdapter) {
            ((PigeonPairAdapter) adapter).setIPigeonPairInterface((pos, c_id, methodId) -> {
                if (methodId == 0) {
                    makePair(pos, c_id);
                } else if (methodId == 1) {
                    setCageKL(pos, c_id);
                }
            });
            adapter.setOnItemLongClickListener(new OnItemLongClickListener() {
                @Override
                public boolean onItemLongClick(@NonNull BaseQuickAdapter adapter, @NonNull View view, int position) {

                    showPobChangeCage(data.get(position).getC_number());
                    return false;
                }
            });

        }


        if (headView != null) {
            spinnerStatus = headView.findViewById(R.id.spiner_status);
            spinnerStatus.setAdapter(new PdStatusAdapter(this, statusItems));
            //spinner初始化时会自动调用一次onItemSelected
            spinnerStatus.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                @Override
                public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                    String key = statusItems.get(position);
                    if (status != maps.get(key)) {
                        status = maps.get(key);
                        loadData(true);
                    }
                }

                @Override
                public void onNothingSelected(AdapterView<?> parent) {

                }
            });
        }
    }

    //调窝
    public void showPobChangeCage(int c_number) {
        DialogHelper.showChangeCageDialog(this, param -> {
            saveCLZT(c_number, param);
        });
    }

    //配对成功
    public void showPdSuccess(int pos) {

        DialogHelper.showPDSuccessDialog(this, (isFd, isKw) -> {
            makePDSuccess(pos, isFd, isKw);
        });
    }


    //存栏操作
    public void saveCLZT(int c_number, CLZTParam param) {
        if (c_number <= 0) {
            return;
        }
        param.setWh_id(wh_id);
        param.setC_number(c_number);
        operatePresenter.changeCLZT(param);
    }

    //设置空笼
    public void setCageKL(int pos, int c_id) {
        current_pos = pos;
        if (data.get(current_pos).getStatus() == CageStatusConfig.STATUS_KL) {
            MyToast.showShort("已经是空笼状态");
            return;
        }
        CageParam cageParam = new CageParam();
        cageParam.setStatus(CageStatusConfig.STATUS_PD);
        cageParam.setC_id(c_id);
        mPresenter.updateCageKL(cageParam);
    }

    //配对
    public void makePair(int pos, int c_id) {
        current_pos = pos;
        CageParam cageParam = new CageParam();
        if (data.get(pos).getStatus() == CageStatusConfig.STATUS_PD) {
            showPdSuccess(current_pos);
        } else {
            cageParam.setStatus(CageStatusConfig.STATUS_PD);
            cageParam.setC_id(c_id);
            cageParam.setC_number(data.get(current_pos).getC_number());
            mPresenter.updateCagePair(cageParam);
        }

    }

    public void makePDSuccess(int pos, boolean isFd, boolean isKw) {
        current_pos = pos;
        CageParam cageParam = new CageParam();
        cageParam.setStatus(CageStatusConfig.STATUS_YGYM);
        cageParam.setC_id(data.get(pos).getC_id());
        cageParam.setProduce_status(isKw ? CageStatusConfig.PRODUCE_KW : CageStatusConfig.PRODUCE_FD);
        cageParam.setC_number(data.get(current_pos).getC_number());
        mPresenter.updateCagePair(cageParam);
    }

    @Override
    public void onQueryCageSuccess(PageInfo<CageBeanVo> bean) {

    }

    @Override
    public void onQueryPairCageSuccess(PageInfo<PairBean> bean) {
        if (bean != null && bean.getList() != null) {
            data.addAll(bean.getList());
        }
        super.onQueryPairCageSuccess(bean);
    }

    @Override
    public void onUpdateCageStatusSuccess(int type) {
        loadData(true);
//        if (current_pos >= 0) {
//            MyToast.showLong("操作成功");
//            allCount = allCount - 1;
//            txtAmount.setText(allCount + "");
//            data.remove(current_pos);
//            adapter.notifyDataSetChanged();
//            current_pos = -1;
//        }

    }

    @Override
    public void onCheckEggSuccess(PageInfo<FertilizeBean> list) {

    }

    @Override
    public void onQueryCateStatusRecordSuccess(List<CageStatusRecordBean> list) {

    }

    @Override
    public void onSaveLayEggSuccess(int c_id) {

    }

    @Override
    public void onAddOperate(int type) {
        loadData(true);


    }

    @Override
    public void onFailed(String error) {

    }
}
